var windowName = "services";
var Page, list
apiready = function () {
    onWindowShow(function () {
    })
    onWindowHide(function () {
    })
    console.log("index page is OK");
    list = new List();
    Page = createVM("#Page", {
        //页面数据
        cur: 0
        , cats: []
        , cat1: -1
        , cat2: -1
        , tag: 0
        , order: ""
        , keywords: ""
        , timeOpen: false
        , sortOpen: false
        , list: []
    }, {
        //页面方法
        "init": function () {
            var data = getData4Page()
            var cats = getLocData("cacheCats")
            this.cats = cats
            logJson(cats, "cats_")
            var page = data.page
            if (isTrue(page)) {
                data.cat1 += 10
            }
            this.cat1 = data.cat1
            this.cat2 = data.cat2;
            var lat = getLocData("lat");
            var lon = getLocData("lon");
            this.lat = data.lat
            this.lon = data.lon
            var data = {lat: this.lat, lon: this.lon, cat1: this.cat1, cat2: this.cat2};
            logJson(data,"data_")
            list.setUrl("/app/products/lists").setCbFunc(function (res) {
                clearLoading()
                logJson(res, "Prod_:")
                Page.list = res;
            });
            reload();
        }
        // ,scroll:function (item) {
        //     location.replace(this.location + "#item" + item);
        // }
        , setCat1: function (index) {
            this.cat1 = index;
            this.cat2 = -1;
            reload();
        }
        , setCat2: function (index) {
            this.cat2 = index;
            reload();
        }
        , setTag: function (tagId) {
            this.tag = tagId;
            reload();

            this.timeOpen = false
            this.sortOpen = false
        }
        , setOrder: function (order) {
            this.order = order;
            reload();

            this.timeOpen = false
            this.sortOpen = false
        }
        ,search:function () {
            reload();
        }
    })
    Page.init()
}

function reload() {
    showLoading()
    var cats = Page.cats;
    var cat1 = cats[Page.cat1];
    var cat2Id = 0;
    if (Page.cat2 != -1) {
        var cat2 = cat1.sons[Page.cat2];
        cat2Id = cat2.id
    }
    var data = {
        lat: Page.lat,
        lon: Page.lon,
        cat1: cat1.id,
        cat2: cat2Id,
        tag: Page.tag,
        order: Page.order,
        keywords: Page.keywords,
    };
    logJson(data,"data_")
    list.setData(data).reload();
}